<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tambahan extends MX_Controller {
  function __construct() {
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('mobile_detect');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('outlet/maktifitas');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
   
  }
  function activity($id_outlet){
    if($this->input->post()){
      $pst = $this->input->post();
      
        $month = (int)date("m", strtotime(str_replace("_","-", $pst['tanggal'])));
        $year = (int)date("Y", strtotime(str_replace("_","-", $pst['tanggal'])));
        $day = (int)date("j", strtotime(str_replace("_","-", $pst['tanggal']))); 
        $id_actifity_report = $this->global_models->get_field("m_actifity_report", "id_actifity_report", array("month" => $month, "year" => $year, "id_outlet" => $pst['id_outlet']));
        $id_report = $this->global_models->get_field("d_day_actifity_report", "id_actifity_report", array("id_actifity_report" => $id_actifity_report, "tanggal" => $day));
        
        if(!empty($id_report)){
        $kirim = array(
            "id_actifity_report"          => $id_actifity_report,
            "tanggal"                     => $pst['tanggal'],
            "service"                     => $pst['service'],
            "service_belanja"             => $pst['service_belanja'],
            "belanja"                     => $pst['belanja'],
            "p_service"                   => $pst['p_service'],
            "p_service_plus"              => $pst['p_service_plus'],
            "p_belanja_plus"              => $pst['p_belanja_plus'],
            "p_belanja"                   => $pst['p_belanja'],
            "total_hpp"                   => $pst['total_hpp'],
            "update_by_users"             => $this->session->userdata("id"),
        );
        $id_day_actifity_report = $this->global_models->update("d_day_actifity_report", array("id_day_actifity_report" => $pst['id_day_actifity_report']), $kirim);
      }
      else{
         $kirim = array(
            "id_actifity_report"          => $id_actifity_report,
            "tanggal"                     => $day,
            "service"                     => $pst['service'],
            "service_belanja"             => $pst['service_belanja'],
            "belanja"                     => $pst['belanja'],
            "p_service"                   => $pst['p_service'],
            "p_service_plus"              => $pst['p_service_plus'],
            "p_belanja_plus"              => $pst['p_belanja_plus'],
            "p_belanja"                   => $pst['p_belanja'],
            "total_hpp"                   => $pst['total_hpp'],
            "create_by_users"             => $this->session->userdata("id"),
            "create_date"                 => date("Y-m-d H:i:s")
        );
        $id_day_actifity_report = $this->global_models->insert("d_day_actifity_report", $kirim);
      }
      redirect("outlet/milik");
    }
    $this->template->build('tambahan/add-activity', 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'id_outlet'    => $id_outlet,
            'title_table'    => "Activity Record",
          ));
    $this->template
      ->set_layout('default')
      ->build('tambahan/add-activity');
  }
  
  function get_data($id_outlet, $tanggal) {
    $date = (int)date("d", strtotime(str_replace("_","-", $tanggal)));
    $month = (int)date("m", strtotime(str_replace("_","-", $tanggal)));
    $year = (int)date("Y", strtotime(str_replace("_","-", $tanggal)));
    $id_actifity_report = $this->global_models->get_field("m_actifity_report", "id_actifity_report", array("month" => $month, "year" => $year, "id_outlet" => $id_outlet));
    $data = $this->global_models->get("d_day_actifity_report", array("id_actifity_report" => $id_actifity_report, "tanggal" => $date));
//    print_r($id_outlet);die;
    $hasil = array(
        "service"               => $data[0]->service,
        "service_belanja"       => $data[0]->service_belanja,
        "belanja"               => $data[0]->belanja,
        "p_service"             => $data[0]->p_service,
        "p_service_plus"        => $data[0]->p_service_plus,
        "p_belanja_plus"        => $data[0]->p_belanja_plus,
        "p_belanja"             => $data[0]->p_belanja,
        "total_hpp"             => $data[0]->total_hpp,
        "id_day_actifity_report"             => $data[0]->id_day_actifity_report,
    );
    print json_encode($hasil);
    die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */